package com.cn.parks.dao.impl;

import com.cn.parks.dao.RoleDao;
import com.cn.parks.entity.Role;
import com.cn.parks.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * 角色数据访问实现类
 * 实现对角色表的增删改查等基本操作
 */
public class RoleDaoImpl implements RoleDao {
    
    /**
     * 添加新角色
     * @param role 角色对象
     * @return 影响的行数，1表示添加成功，0表示添加失败
     * @throws Exception SQL执行异常
     */
    @Override
    public int insert(Role role) throws Exception {
        String sql = "INSERT INTO role (role_id, role_name) VALUES (?, ?)";
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, role.getRoleId());
            pstmt.setString(2, role.getRoleName());
            return pstmt.executeUpdate();
        } finally {
            if (pstmt != null) pstmt.close();
            if (conn != null) DBUtil.closeConnection(conn);
        }
    }

    /**
     * 根据角色ID删除角色
     * @param roleId 角色ID
     * @return 影响的行数，1表示删除成功，0表示删除失败
     * @throws Exception SQL执行异常
     */
    @Override
    public int deleteById(String roleId) throws Exception {
        String sql = "DELETE FROM role WHERE role_id = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, roleId);
            return pstmt.executeUpdate();
        } finally {
            if (pstmt != null) pstmt.close();
            if (conn != null) DBUtil.closeConnection(conn);
        }
    }

    /**
     * 更新角色信息
     * @param role 角色对象
     * @return 影响的行数，1表示更新成功，0表示更新失败
     * @throws Exception SQL执行异常
     */
    @Override
    public int update(Role role) throws Exception {
        String sql = "UPDATE role SET role_name = ? WHERE role_id = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, role.getRoleName());
            pstmt.setString(2, role.getRoleId());
            return pstmt.executeUpdate();
        } finally {
            if (pstmt != null) pstmt.close();
            if (conn != null) DBUtil.closeConnection(conn);
        }
    }

    /**
     * 根据角色ID查询角色信息
     * @param roleId 角色ID
     * @return 角色对象，如果不存在返回null
     * @throws Exception SQL执行异常
     */
    @Override
    public Role getById(String roleId) throws Exception {
        String sql = "SELECT * FROM role WHERE role_id = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, roleId);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                return extractRoleFromResultSet(rs);
            }
            return null;
        } finally {
            if (rs != null) rs.close();
            if (pstmt != null) pstmt.close();
            if (conn != null) DBUtil.closeConnection(conn);
        }
    }

    /**
     * 查询所有角色信息
     * @return 角色列表
     * @throws Exception SQL执行异常
     */
    @Override
    public List<Role> getAll() throws Exception {
        String sql = "SELECT * FROM role";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Role> list = new ArrayList<>();
        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                list.add(extractRoleFromResultSet(rs));
            }
            return list;
        } finally {
            if (rs != null) rs.close();
            if (pstmt != null) pstmt.close();
            if (conn != null) DBUtil.closeConnection(conn);
        }
    }

    /**
     * 根据角色名称查询角色信息
     * @param roleName 角色名称
     * @return 角色对象，如果不存在返回null
     * @throws Exception SQL执行异常
     */
    @Override
    public Role getByRoleName(String roleName) throws Exception {
        String sql = "SELECT * FROM role WHERE role_name = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, roleName);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                return extractRoleFromResultSet(rs);
            }
            return null;
        } finally {
            if (rs != null) rs.close();
            if (pstmt != null) pstmt.close();
            if (conn != null) DBUtil.closeConnection(conn);
        }
    }

    /**
     * 从ResultSet中提取Role对象的辅助方法
     * @param rs ResultSet结果集
     * @return Role对象
     * @throws Exception SQL执行异常
     */
    private Role extractRoleFromResultSet(ResultSet rs) throws Exception {
        Role role = new Role();
        role.setRoleId(rs.getString("role_id"));
        role.setRoleName(rs.getString("role_name"));
        return role;
    }
} 